package edu.ccut.saturn.component.loader.node;

import java.util.ArrayList;
import java.util.List;

import org.dom4j.Element;
import org.osgi.framework.Bundle;

import edu.ccut.saturn.component.IDataMappingRuleInfo;
import edu.ccut.saturn.component.INodeInfo;
import edu.ccut.saturn.component.SaturnComponentException;
import edu.ccut.saturn.component.impl.SaturnDataMappingRuleInfo;
import edu.ccut.saturn.component.impl.node.SaturnDataMappingNodeInfo;

/**
 * 用于解析logic组件配置文件中的“数据映射”结点的加载类。
 * <p>
 * 继承了<code>NodeInfoLoader</code>,并实现了<code>loadNode</code>方法。
 * <p>
 * 
 * @author 温建
 * @version 1.0 2008.8.15
 * 
 * @see <code>SaturnDataMappingNodeInfo</code>
 */

public class DataMappingNodeInfoLoader extends NodeInfoLoader {

	public static final String KEY_NODE_SATURNDATA_MAPPING = "saturndata-mapping";

	public static final String KEY_NODE_TARGET = "target";

	public static final String KEY_TARGET_VALUE_KEY = "value-key";

	public static final String KEY_TARGET_FULLKEY = "fullkey";

	public static final String KEY_NODE_MAPPING_RULES = "mapping-rules";

	public static final String KEY_NODE_RULE = "rule";

	public static final String KEY_RULE_KEY = "key";

	public static final String KEY_RULE_SOURCE = "source";

	public static final String KEY_RULE_FULLKEY = "fullkey";

	public static final String KEY_RULE_VALUE = "value";

	@SuppressWarnings("unchecked")
	public List<INodeInfo> loadNode(Element root, Bundle bundle)
			throws SaturnComponentException {

		List<Element> mappingNodes = root.elements(KEY_NODE_SATURNDATA_MAPPING);
		List<INodeInfo> nodeInfos = new ArrayList<INodeInfo>();

		for (Element mappingNode : mappingNodes) {
			String id = mappingNode.attributeValue(KEY_NODE_ID);
			String x = mappingNode.attributeValue(KEY_NODE_X).trim();
			String y = mappingNode.attributeValue(KEY_NODE_Y).trim();
			String nextId = mappingNode.attributeValue(KEY_NODE_NEXT);
			String name = mappingNode.elementTextTrim(KEY_NODE_NAME);
			String description = mappingNode.elementText(KEY_NODE_DESCRIPTION);
			Element targetNode = mappingNode.element(KEY_NODE_TARGET);
			String targetValueKey = targetNode
					.attributeValue(KEY_TARGET_VALUE_KEY);

			String targetFullKey = targetNode
					.attributeValue(KEY_TARGET_FULLKEY);

			List<Element> rules = mappingNode.element(KEY_NODE_MAPPING_RULES)
					.elements(KEY_NODE_RULE);

			List<IDataMappingRuleInfo> ruleInfos = new ArrayList<IDataMappingRuleInfo>();

			for (Element rule : rules) {
				String key = rule.attributeValue(KEY_RULE_KEY);
				String value = rule.attributeValue(KEY_RULE_VALUE);
				String source = rule.attributeValue(KEY_RULE_SOURCE);
				String fullKey = rule.attributeValue(KEY_RULE_FULLKEY);
				IDataMappingRuleInfo ruleInfo = new SaturnDataMappingRuleInfo(
						key, value, source, fullKey);

				ruleInfos.add(ruleInfo);
			}
			INodeInfo dataMappingNodeInfo = new SaturnDataMappingNodeInfo(id,
					name, description, nextId, null, targetValueKey,
					targetFullKey, ruleInfos, x, y);

			nodeInfos.add(dataMappingNodeInfo);
		}
		return nodeInfos;
	}
}
